﻿using Accessibility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    /// <summary>
    /// 对Messenger窗口进行操作
    /// </summary>
    public class QqWindowHelper
    {
        IntPtr _QqWindowHandle;
        string _winTitle;
        IAccessible _inputBox;

        public QqWindowHelper(IntPtr windowHandle, String winTitle)
        {
            _QqWindowHandle = windowHandle;
            _winTitle = winTitle;

            GetAccessibleObjects(_QqWindowHandle, out _inputBox);
        }

        /// <summary>
        /// 返回消息框内容
        /// </summary>
        /// <returns></returns>
        public string GetContent()
        {
            if (_inputBox == null)
            {
                return null;
            }
            string value = (string)_inputBox.get_accValue(Win32.CHILDID_SELF);
            return value;
        }

        private IAccessible[] GetAccessibleChildren(IAccessible paccContainer)
        {
            IAccessible[] rgvarChildren = new IAccessible[paccContainer.accChildCount];
            int pcObtained;
            Win32.AccessibleChildren(paccContainer, 0, paccContainer.accChildCount, rgvarChildren, out pcObtained);
            return rgvarChildren;

        }
        //按层级找到对象
        public IAccessible GetAccessibleChild(IAccessible paccContainer, int[] array)
        {
            if (array.Length > 0)
            {
                IAccessible result = GetAccessibleChildren(paccContainer)[array[0]];

                int[] array_1 = new int[array.Length - 1];
                for (int i = 0; i < array.Length - 1; i++)
                {
                    array_1[i] = array[i + 1];
                }
                return GetAccessibleChild(result, array_1);
            }
            else
            {
                return paccContainer;
            }
        }

        private void GetAccessibleObjects(System.IntPtr imWindowHwnd, out IAccessible inputBox)
        {
            Guid guidCOM = new Guid(0x618736E0, 0x3C3D, 0x11CF, 0x81, 0xC, 0x0, 0xAA, 0x0, 0x38, 0x9B, 0x71);
            Accessibility.IAccessible IACurrent = null;

            Win32.AccessibleObjectFromWindow(imWindowHwnd, (int)Win32.OBJID_CLIENT, ref guidCOM, ref IACurrent);
            IACurrent = (IAccessible)IACurrent.accParent;
            inputBox = null;
            //      inputBox = GetAccessibleChild(IACurrent, new int[] { 3, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 3, 0 });

            int childCount = IACurrent.accChildCount;
            object[] windowChildren = new object[childCount];
            int pcObtained;
            Win32.AccessibleChildren(IACurrent, 0, childCount, windowChildren, out pcObtained);

            printChild(IACurrent, "");


        }

        private void printChild(IAccessible child, String tag)
        {
            Console.WriteLine("----------------------" + tag);

            IAccessible[] accArr = new IAccessible[child.accChildCount];
            int pcObtained;
            Win32.AccessibleChildren(child, 0, child.accChildCount, accArr, out pcObtained);
            for (int i = 0; i < accArr.Length; i++)
            {
                tag = tag + "-" + i;
                IAccessible item = accArr[i];
                if (item.GetType().ToString().Contains("32"))
                {
                  Console.WriteLine(tag + ":" + "text--Int32");
                 //   System.IO.File.WriteAllText("D:\\1.txt", tag + ":" + "text--Int32");
                }
                else
                {
       
                    Console.WriteLine(tag + ":" + "[accChildCount]" + item.accChildCount);
                    Console.WriteLine(tag + ":" + "[_accRole]" + item.get_accRole(0));
                    Console.WriteLine(tag + ":" + "[accName]" + item.get_accName(0));
                    Console.WriteLine(tag + ":" + "[accValue]" + item.get_accValue(0));
                    Console.WriteLine(tag + ":" + "[accDescription]" + item.get_accDescription(0));
                 //   Console.WriteLine(tag + ":" + "[accDescription]" + item.get);

                    if ((item.get_accName(0) + "").Contains("搜索"))
                    {
                        //Console.WriteLine(tag + ":" + item.accRole);


                    }
                    printChild(item, tag);
                }
            }
        }

    }

    public class Console
    {
        public static void WriteLine(String str) {
            FileStream fs = new FileStream("d:\\1.txt", FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
         //   fs.SetLength(0);//首先把文件清空了。
            sw.Write(str+"\n");//写你的字符串。
            sw.Close();
        }

     
    }


}
